package mr.fresh.serivce.manage.good;

import com.alibaba.druid.sql.visitor.functions.Nil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import mr.fresh.api.CommonPage;
import mr.fresh.api.CommonResult;
import mr.fresh.entity.manange.FreshGoods;
import mr.fresh.entity.manange.FreshPic;
import mr.fresh.entity.manange.FreshPicExample;
import mr.fresh.entity.request.FindListGoodRequest;
import mr.fresh.entity.request.SaveGoodRequest;
import mr.fresh.entity.response.FindListGoodResponse;
import mr.fresh.entity.tool.NoUtli;
import mr.fresh.mapper.manage.FreshGoodsMapper;
import mr.fresh.mapper.manage.FreshPicMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @CLassName FreshProductService
 * @Description 商品服务实体类
 * @Author mr.jl
 * @Date 2019/5/27 21:58
 * @Version 1.0
 **/
@Slf4j
@Service
public class FreshGoodsService {

    @Autowired
    private FreshGoodsMapper goodsMapper;

    @Autowired
    private FreshPicMapper picMapper;

    /**
     * @Description: 条件查询商品基本信息 代码实现
     * @Param: [model] 条件
     * @return: java.util.List<mr.fresh.entity.manange.FreshGoods>
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    public CommonPage<FreshGoods> findList(FindListGoodRequest model) {
        //根据数据模型，查询符合条件的商品集合
        PageHelper.startPage(model.getPage(), model.getSize());
        List<FreshGoods> goods = goodsMapper.selectByFindListGoodRequest(model);
       /* ArrayList<FindListGoodResponse> response = new ArrayList<FindListGoodResponse>();
        //遍历商品集合,查每个商品的图片,然后把商品信息和图片信息都塞入返回值里面
        goods.forEach(item -> response.add(getFindListGoodResponse(item)));
        CommonPage<FindListGoodResponse> page = CommonPage.restPage(response);*/
        return CommonPage.restPage(goods);

    }

    /**
     * @Description: 把数据模型的数据复制，并且创建商品编号
     * @Param: [model]
     * @return: mr.fresh.entity.manange.FreshGoods
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    private FreshGoods copyProperties(SaveGoodRequest request) {
        FreshGoods goods = new FreshGoods();
        BeanUtils.copyProperties(request, goods);
        String goodNo = "gs" + NoUtli.makeNo();
        goods.setGoodNo(goodNo);
        return goods;
    }

    /**
     * @Description: 创建商品基本信息
     * @Param: [t]
     * @return: java.lang.Integer
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Transactional(rollbackFor = Exception.class)
    public CommonResult saveGoods(SaveGoodRequest request) {
        FreshGoods goods = this.copyProperties(request);
        goods.setCreateUser(1);
        goods.setCreateTime(new Date());
        Integer integer = goodsMapper.insert(goods);
        request.getPicList().forEach(item -> {
            item.setGoodsId(goods.getGoodId());
            picMapper.insert(item);
        });
        return integer != null && integer > 0 ? CommonResult.success() : CommonResult.failed();
    }


    /**
     * @Description: 更新商品基本信息
     * @Param: [t]
     * @return: java.lang.Integer
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateGoods(SaveGoodRequest request) {
        FreshGoods goods = this.copyProperties(request);
        goods.setUpdateUser(1);
        goods.setUpdateTime(new Date());
        Integer integer = goodsMapper.updateByPrimaryKeySelective(goods);
        picMapper.deleteByGoodId(request.getGoodId());
        request.getPicList().forEach(item -> {
            item.setGoodsId(goods.getGoodId());
            picMapper.insert(item);
        });
        return integer != null && integer > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * @Description: 根据id查询商品信息
     * @Param: [id]
     * @return: mr.fresh.entity.manange.FreshGoods
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    public FindListGoodResponse findById(Long id) {
        FreshGoods item = goodsMapper.selectByPrimaryKey(id);
        if (item != null) {
            return getFindListGoodResponse(item);
        }
        return null;
    }

    /**
     * @Description: 根据商品id，返回FindListGoodResponse的结果集
     * @Param: [item]
     * @return: mr.fresh.entity.response.FindListGoodResponse
     * @Author: Mr.jl
     * @Date: 2019/6/10
     */
    private FindListGoodResponse getFindListGoodResponse(FreshGoods item) {
        List<FreshPic> freshPics = picMapper.selectByGoodId(item.getGoodId());
        FindListGoodResponse goodResponse = new FindListGoodResponse();
        ArrayList<FreshPic> picList = new ArrayList<FreshPic>();
        BeanUtils.copyProperties(item, goodResponse);
        if (!freshPics.isEmpty()) {
            freshPics.forEach(pic -> picList.add(pic));
            goodResponse.setList(picList);
            return goodResponse;
        }
        return goodResponse;
    }

    /**
     * @Description: 根据id删除商品信息
     * @Param: [id]
     * @return: mr.fresh.entity.manange.FreshGoods
     * @Author: Mr.jl
     * @Date: 2019/6/8
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteById(Long id) {
        int goodsDelete = goodsMapper.deleteByPrimaryKey(id);
        FreshPicExample example = new FreshPicExample();
        example.createCriteria().andGoodsIdEqualTo(id);
        int picDelete = picMapper.deleteByExample(example);
        return picDelete;
    }
}
